package com.lindroy.androidplayer.viewmodels

import androidx.lifecycle.viewModelScope
import androidx.paging.cachedIn
import com.lindroy.androidplayer.logic.network.repository.Repository
import com.lindroy.androidplayer.utils.SingleLiveEvent
import com.lindroy.androidplayer.utils.mapItem
import com.lindroy.androidplayer.utils.mapSingle

/**
 * @author Lin
 * @date 2022/1/22
 * @function
 */
class WxArticleViewModel : PageViewModel() {

    private val itemClickAction = SingleLiveEvent<ArticleItemViewModel>()

    val itemClickLiveData = itemClickAction.mapSingle { it }

    fun getWxArticles(id: Int) = Repository.getWxArticles(id).mapItem {
        ArticleItemViewModel(
            apkLink = it.apkLink,
            author = it.author,
            chapterName = it.chapterName,
            collect = it.collect,
            fresh = it.fresh,
            id = it.id,
            link = it.link,
            niceDate = it.niceDate,
            shareUser = it.shareUser,
            superChapterName = it.superChapterName,
            title = it.title,
            type = it.type,
            itemCLickListener = itemClickAction
        )
    }.cachedIn(viewModelScope)
}